//
//  StatusCellRetweetedView.swift
//  0110-weibo
//
//  Created by 鲍志刚 on 16/1/20.
//  Copyright © 2016年 zhongye. All rights reserved.
//

import UIKit
import SnapKit

class StatusCellRetweetedView: UIView {
    
    //定义约束对象属性 来进行记录 Constraint 就是SnapKit中约束对象的类型
    
    var bottomConstant: Constraint?

    
    //mark: 定义外部变量
    var retweeted_status: Status? {
        didSet{
            //绑定数据
            retweetedLabel.text = retweeted_status?.text
            
            //通过这句代码 在没有配图的情况下 将配图视图的大小计算为 CGSizeZero
            pictureView.imageURLs = retweeted_status?.imageURLs
            
            //1.卸载约束
            self.bottomConstant?.uninstall()
            
            //更行约束
            if let urls = retweeted_status?.imageURLs{
                
                print(urls.count)
                //设置底部约束相对于配图视图的底部
                self.snp_updateConstraints(closure: { (make) -> Void in
                        self.bottomConstant = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            } else {
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //设置底部约束相对于转发微博正文
                    self.bottomConstant = make.bottom.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
                
                
            }
            
        }
        
    }
    
    //找到自定义的入口
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
        self.backgroundColor = UIColor.whiteColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK:设置视图
    private func setupUI(){
        
        //手写代码创建的view背景色 是 透明
        backgroundColor = UIColor(white: 0.9, alpha: 1)
        addSubview(retweetedLabel)
        addSubview(pictureView)
        
        //设置约束
        retweetedLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(retweetedLabel.snp_left)
            make.top.equalTo(retweetedLabel.snp_bottom).offset(StatusCellMargin)
        }
        
        //添加自动计算转发微博的行高一条约束
        self.snp_makeConstraints { (make) -> Void in
           self.bottomConstant = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
        
        
    }
    
    //MARK: 懒加载视图
    private lazy var retweetedLabel: UILabel = UILabel(title: "终于等到你，还好我没放弃", fontSize: 14, color: UIColor.darkGrayColor(), margin: StatusCellMargin)

    private lazy var pictureView: StatusCellPictureView = StatusCellPictureView()
}
